package tpi.gui.carmensd.ui

import android.app.Activity
import android.os.Bundle
import org.eclipse.xtend.lib.annotations.Accessors
import retrofit.RestAdapter
import tpi.gui.carmensd.R
import tpi.gui.carmensd.dto.Pais
import tpi.gui.carmensd.service.CarmenSandiegoService
import android.widget.ArrayAdapter
import android.widget.Spinner
import retrofit.Callback
import retrofit.RetrofitError
import retrofit.client.Response
import android.view.View
import android.content.Intent
import tpi.gui.carmensd.utils.Utils
import android.widget.Button
import tpi.gui.carmensd.dto.OrdenDeArresto
import android.widget.TextView
import tpi.gui.carmensd.dto.Villano
import java.util.List

@Accessors
class InterpolActivity extends Activity {
	
	List<Villano> villanos
	Pais paisActual
	Villano acusado
	CarmenSandiegoService service
	extension Utils = new Utils
	
	override void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_interpol)
              
        service = createService()
        pedidoPaisActual[| botonPaisActual() ]
        pedidoSospechosos[| cargarSospechosos() ]
        
    }
    
    def pedidoSospechosos(()=>void action) {
    	service.getVillanos(new Callback<List<Villano>>() {	
						override failure(RetrofitError e) {
							e.printStackTrace
						}
						override success(List<Villano> villanos, Response response) {
							setVillanos(villanos)
							action.apply()
						}
    	})
    }

    def pedidoPaisActual(()=>void action) {
    	service.getPaisActual(new Callback<Pais>() {	
						override failure(RetrofitError e) {
							e.printStackTrace
						}
						override success(Pais pais, Response response) {
							setPaisActual(pais)
							action.apply()
						}
    	})
    }
    
    def emitirOrden(View view) {
    	val spinner = findViewById(R.id.sospechosos) as Spinner
        service.emitirOrden(spinner.selectedItem as String, 
        				new Callback<Villano>(){			
									override failure(RetrofitError e) {
										e.printStackTrace
									}
									override success(Villano villano, Response response) {
										efectuarEmicion()
									}
						}
		)
    } 
    
    def irAlPaisActual(View view) {
    	new Intent(this, PistasActivity).startActivity()
    }
    
    def efectuarEmicion() {
		new Intent(this, PistasActivity).startActivity()
    }
    
    def cargarSospechosos() {
		findViewById(R.id.sospechosos) as Spinner => [
			adapter = new ArrayAdapter<String>(this,
	                               android.R.layout.simple_spinner_item,
	                               villanos.toStringList("nombre"))
        ]
	}
	
	def botonPaisActual() {
		findViewById(R.id.paisActual) as Button => [
        	text = paisActual.nombre
        ]
	}
	
	def ordenDeArresto(()=>void action) {
		service.getAcusado(
	       	new Callback<Villano>() {
	       		override failure(RetrofitError e) {
	       			e.printStackTrace
	       		}
	       		override success(Villano villano, Response response) {
	       			setAcusado(villano)
	       			action.apply()
	       		}
	       	}
		)
	}

	def irAlAeropuerto(View view) {
		new Intent(this, AeropuertoActivity).startActivity()
	}
	
	def mostrarPantallaFinal() {
//		new Intent(this, PantallaFinalActivity).startActivity()
	}
	
}